<template>
  <div class="app-container">
    <div class="view-title">系统信息管理</div>
    <el-button
      type="primary"
      @click="handlerAdd"
      v-permission="['systemInformation:add']"
      >新增</el-button
    >

    <!--    table-->

    <el-table
      :data="personalTableData"
      v-loading="loading"
      element-loading-text="正在加载中"
      :header-cell-style="{
        borderColor: '#F0F0F0',
        height: '51px',
        background: '#F0F0F0',
        color: '#333',
      }"
      :row-style="{ height: '51px' }"
      :cell-style="{ borderColor: '#F0F0F0' }"
      style="width: 100%; margin-top: 20px"
    >
      <el-table-column prop="text" label="系统信息内容" align="center">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" align="center">
      </el-table-column>
      <el-table-column prop="updated_at" label="更新时间" align="center">
      </el-table-column>

      <el-table-column fixed="right" label="操作" width="180" align="center">
        <template slot-scope="scope">
          <el-button
            @click="handleCheck(scope.row)"
            type="text"
            size="small"
            v-permission="['systemInformation:view']"
            >查看</el-button
          >
          <el-button
            @click="handlerEdit(scope.row)"
            type="text"
            size="small"
            v-permission="['systemInformation:edit']"
            >修改</el-button
          >
          <el-button
            @click="handlerDelete(scope.row)"
            type="text"
            size="small"
            v-permission="['systemInformation:delete']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <div class="page_box">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNow"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>

    <!--      新增弹窗-->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisibleAdd"
      width="30%"
      :close-on-click-modal="false"
      @close="handlerCloseAdd"
    >
      <div class="dialog_add_box">
        <el-form
          :model="systemRuleForm"
          :rules="rules"
          ref="ruleFormSystem"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="系统内容" prop="text">
            <el-input v-model="systemRuleForm.text"></el-input>
          </el-form-item>

          <el-form-item label="可见时间" prop="visibility_time">
            <!--              <el-input v-model="systemRuleForm.text"></el-input>-->
            <el-date-picker
              v-model="systemRuleForm.visibility_time"
              type="datetime"
              placeholder="选择可见时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              @change="handlerChangeDate"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancelDialogAdd('ruleFormSystem')">取 消</el-button>
        <el-button type="primary" @click="sureDialogAdd('ruleFormSystem')"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!--      查看一条数据弹窗-->
    <el-dialog
      title="查看系统消息"
      :visible.sync="dialogVisibleCheck"
      width="30%"
    >
      <div>
        <el-table :data="tableDataCheckSystem" style="width: 100%">
          <el-table-column prop="text" label="系统信息内容"> </el-table-column>
          <el-table-column prop="created_at" label="创建时间">
          </el-table-column>
          <el-table-column prop="updated_at" label="更新时间">
          </el-table-column>
        </el-table>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getAppSysMsgList,
  getAppSysMsgInfo,
  addAppSysMsg,
  updateAppSysMsg,
  deleteAppSysMsg,
} from "@/api/systemInformation.js";

export default {
  name: "personalInformation",
  data() {
    return {
      personalTableData: [],
      pageNow: 1,
      pageSize: 10,
      total: null,
      dialogVisibleAdd: false,
      systemRuleForm: {
        text: "",
        visibility_time: "",
      },
      rules: {
        text: [{ required: true, message: "请输入系统信息", trigger: "blur" }],
        visibility_time: [
          { required: true, message: "请选择可见时间", trigger: "change" },
        ],
      },
      deleteId: "",
      editId: "",
      checkId: "",
      dialogVisibleCheck: false,
      tableDataCheckSystem: [],
      loading: true,
      dialogTitle: "",
    };
  },
  mounted() {
    this.getAppSysMsgList();
  },
  methods: {
    // 获取系统信息列表
    getAppSysMsgList() {
      getAppSysMsgList({
        is_visibility_time: 0,
        page_now: this.pageNow,
        page_size: this.pageSize,
      }).then((res) => {
        if (res.code == 1) {
          this.loading = false;
          this.personalTableData = res.data;
          this.total = res.page_info.total_count;
        } else {
          this.loading = false;
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 添加系统信息
    addAppSysMsg() {
      addAppSysMsg({
        text: this.systemRuleForm.text,
        visibility_time: this.systemRuleForm.visibility_time,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppSysMsgList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 删除系统消息
    deleteAppSysMsg() {
      deleteAppSysMsg({
        ids: this.deleteId,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.getAppSysMsgList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 修改系统消息
    updateAppSysMsg() {
      updateAppSysMsg({
        id: this.editId,
        text: this.systemRuleForm.text,
        visibility_time: this.systemRuleForm.visibility_time,
      }).then((res) => {
        if (res.code == 1) {
          this.$message.success(res.msg);
          this.dialogVisibleAdd = false;
          this.getAppSysMsgList();
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 查看一条系统消息
    getAppSysMsgInfo() {
      getAppSysMsgInfo({
        id: this.checkId,
      }).then((res) => {
        if (res.code == 1) {
          const arr = [];
          arr.push(res.data);
          this.tableDataCheckSystem = arr;
          this.dialogVisibleCheck = true;
        } else {
          this.$message.error(res.msg);
          return;
        }
      });
    },

    // 新增
    handlerAdd() {
      this.dialogTitle = "新增";
      this.dialogVisibleAdd = true;
    },

    // 新增弹窗叉号
    handlerCloseAdd() {
      this.systemRuleForm.text = "";
      this.systemRuleForm.visibility_time = "";
      this.dialogVisibleAdd = false;
    },

    // 新增弹窗确定
    sureDialogAdd(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId) {
            this.updateAppSysMsg();
          } else {
            this.addAppSysMsg();
          }
        } else {
          return false;
        }
      });
    },

    // 新增弹窗取消
    cancelDialogAdd(formName) {
      this.systemRuleForm.text = "";
      this.systemRuleForm.visibility_time = "";
      this.dialogVisibleAdd = false;
    },

    // 修改时间
    handlerChangeDate() {},

    // 查看
    handleCheck(val) {
      this.checkId = val.id;
      this.getAppSysMsgInfo();
    },
    // 修改
    handlerEdit(val) {
      this.dialogTitle = "编辑";
      this.editId = val.id;
      this.systemRuleForm = val;
      this.dialogVisibleAdd = true;
    },

    // 删除
    handlerDelete(val) {
      this.deleteId = val.id;
      this.$confirm("此操作将删除该系统信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteAppSysMsg();
        })
        .catch(() => {});
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getAppSysMsgList();
    },
    handleCurrentChange(val) {
      this.pageNow = val;
      this.pageSize = 10;
      this.getAppSysMsgList();
    },
  },
};
</script>

<style scoped lang="scss"></style>
